package com.mlnx.stream.service.iml;

import com.mlnx.common.utils.MyLog;
import com.mlnx.data.entity.BpInfo;
import com.mlnx.data.entity.SpoInfo;
import com.mlnx.data.entity.SugarInfo;
import com.mlnx.data.service.BpDataService;
import com.mlnx.device.pojo.dto.DeviceStateRecord;
import com.mlnx.device.pojo.dto.EcgInfoMsg;
import com.mlnx.service.base.config.PoolConfig;
import com.mlnx.stream.listener.BpListener;
import com.mlnx.stream.listener.EcgListener;
import com.mlnx.stream.listener.SpoListener;
import com.mlnx.stream.listener.SugarListener;
import com.mlnx.stream.service.DeviceBroadcastService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @author zzb
 * @create 2019/10/23 14:25
 */
@Service
public class DeviceBroadcastServiceImpl implements DeviceBroadcastService {

    private MyLog log = MyLog.getLog(getClass());

    private ExecutorService executorService;

    @Autowired(required = false)
    private List<EcgListener> ecgListeners;

    @Autowired(required = false)
    private List<BpListener> bpListeners;

    @Autowired(required = false)
    private List<SpoListener> spoListeners;

    @Autowired(required = false)
    private List<SugarListener> sugarListeners;

    @Autowired
    private BpDataService bpDataService;

    @Autowired
    private PoolConfig poolConfig;

    private AtomicInteger ecgNotProcessCount = new AtomicInteger(0);

    @PostConstruct
    public void init() {
        executorService = poolConfig.broadCastExecutorService();
    }


    @Override
    public int getEcgNotProcessCount() {
        return ecgNotProcessCount.get();
    }

    @Override
    public void deviceOnline(String deviceId) {

    }

    @Override
    public void castEcgInfo(EcgInfoMsg ecgInfoMsg) {

        ecgNotProcessCount.incrementAndGet();

        executorService.submit(new Runnable() {
            @Override
            public void run() {

                if (ecgListeners != null) {
                    for (EcgListener ecgListener : ecgListeners) {
                        try {
                            ecgListener.receiveEcgInfo(ecgInfoMsg);
                        } catch (Exception e) {
                            log.error(e, "分发心电信息异常 {}", ecgListener.toString());
                        }
                    }
                }
                ecgNotProcessCount.decrementAndGet();

            }
        });
    }

    @Override
    public void castStartEcg(Integer patientId, String deviceId) {
        executorService.submit(new Runnable() {
            @Override
            public void run() {

                if (ecgListeners != null) {
                    for (EcgListener ecgListener : ecgListeners) {
                        try {
                            ecgListener.startEcgPacket(patientId, deviceId);
                        } catch (Exception e) {
                            log.error(e, "分发开始心电异常 {}", ecgListener.toString());
                        }
                    }
                }

            }
        });
    }

    @Override
    public void castStopEcg(Integer patientId, String deviceId) {
        executorService.submit(new Runnable() {
            @Override
            public void run() {

                if (ecgListeners != null) {
                    for (EcgListener ecgListener : ecgListeners) {
                        try {
                            ecgListener.stopEcgPacket(patientId, deviceId);
                        } catch (Exception e) {
                            log.error(e, "分发开始心电异常 {}", ecgListener.toString());
                        }
                    }
                }

            }
        });
    }

    @Override
    public void castDeviceState(DeviceStateRecord deviceStateRecord) {
        executorService.submit(new Runnable() {
            @Override
            public void run() {

                if (ecgListeners != null) {
                    for (EcgListener ecgListener : ecgListeners) {
                        try {
                            ecgListener.castDeviceState(deviceStateRecord);
                        } catch (Exception e) {
                            log.error(e, "分发设备状态异常 {}", ecgListener.toString());
                        }
                    }
                }

            }
        });
    }

    @Override
    public void castXmbpElectric(Integer patientId, Integer batteryLevel) {

    }

    @Override
    public void savaSynXmbp(BpInfo bpInfo) {
        bpDataService.save(bpInfo);
    }

    @Override
    public void castBpInfo(BpInfo bpInfo) {

        executorService.submit(new Runnable() {
            @Override
            public void run() {

                if (bpListeners != null) {
                    for (BpListener bpListener : bpListeners) {
                        try {
                            bpListener.receiveBpInfo(bpInfo);
                        } catch (Exception e) {
                            log.error(e, "分发xmbp血压结果异常 {}", bpListener.toString());
                        }
                    }
                }

            }
        });
    }

    @Override
    public void castSpoInfo(SpoInfo spoInfo) {
        executorService.submit(new Runnable() {
            @Override
            public void run() {

                if (spoListeners != null) {
                    for (SpoListener spoListener : spoListeners) {
                        try {
                            spoListener.receiveSpoInfo(spoInfo);
                        } catch (Exception e) {
                            log.error(e, "分发spo结果异常 {}", spoListener.toString());
                        }
                    }
                }

            }
        });
    }

    @Override
    public void castSugarInfo(SugarInfo sugarInfo) {
        executorService.submit(new Runnable() {
            @Override
            public void run() {

                if (spoListeners != null) {
                    for (SugarListener sugarListener : sugarListeners) {
                        try {
                            sugarListener.receiveSugarInfo(sugarInfo);
                        } catch (Exception e) {
                            log.error(e, "分发sugar结果异常 {}", sugarInfo.toString());
                        }
                    }
                }

            }
        });
    }

    public static void main(String[] args) {
        MyLog log = MyLog.getLog(DeviceBroadcastServiceImpl.class);
        log.error(new RuntimeException("xxx"), "分发sugar结果异");
    }
}
